import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import utils from '@/utils/utils'

const user = {
  state: {
    token: getToken(),
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    dept:'',
    deptcode:'',
    userCode: '',
    adCd:'',
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_DEPT: (state, dept) => {
      state.dept = dept 
    },
    SET_CODE: (state, deptcode) => {
      state.deptcode = deptcode 
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USERCODE: (state, userCode) => {
      state.userCode = userCode
    },
    SET_USERADCD: (state, adCd) => {
      state.adCd = adCd
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const username = userInfo.username.trim()
      return new Promise((resolve, reject) => {
        login(userInfo).then(res => {
          if( res.status == '200' ){
            setToken(res.data[0].token)
            commit('SET_USERCODE', username)
            commit('SET_TOKEN', res.data[0].token)
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => { 
          let user = res.data; 
          let url = ''
          if(user.headPortrait){
            url = utils.cutOutURL(user.headPortrait);
          }
          const avatar = !user.headPortrait ? require("@/assets/image/profile.jpg") :  url;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
            
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          commit('SET_NAME', user.userName)
          
          localStorage.setItem('userCode', user.userCode)
          commit('SET_USERCODE', user.userCode)
          commit('SET_DEPT', user.deptName)
          commit('SET_CODE', user.deptCode)
          commit('SET_USERADCD', user.adCd)
          commit('SET_AVATAR', avatar)
          resolve(res)
        }).catch(error => { 
          reject(error)
        })
      })
    },
    // 退出系统
    LogOut({ commit }) {
      return new Promise((resolve, reject) => {
        logout().then((res) => {
          commit('SET_TOKEN', '')
          commit('SET_ROLES', [])
          commit('SET_PERMISSIONS', [])
          removeToken()
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
